﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Catering.Catering_Page.Caipin
{
    public partial class caipin_Singe : System.Web.UI.Page
    {
       
        #region 韦琪部分
        View_Operaction_Common voc = new View_Operaction_Common();
        Catering_Rules.T_RawMaterials crrm = new Catering_Rules.T_RawMaterials();
        Catering.View_Operaction.Caipin_Manage_Operaction cmo = new View_Operaction.Caipin_Manage_Operaction();
        Catering_Rules.DishNutrien CRDN = new Catering_Rules.DishNutrien();
        Catering_Rules.T_Dish _BllDish = new Catering_Rules.T_Dish();
        Catering_Model.E_Dish Mdh = new Catering_Model.E_Dish();
        Catering_Rules.T_DishInfo _BllDishInfo = new Catering_Rules.T_DishInfo();
        List<Catering_Model.DishNutrien> mdnlist = new List<Catering_Model.DishNutrien>();
        Catering_Model.E_DishInfo _ModelDishInfo;
        //菜谱库
        public static string strLibrary = string.Empty;
        private static int i = 0;

        public static string strFilePath = string.Empty;
        public static DataTable dt = new Catering_DataSet.RawInfoDataTable();
        public static string zhi;
        public static string strTableHtml = string.Empty;

        public static string strTreeview = string.Empty;
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            strTreeview = voc.UIDataBing_RawMaterials();
            zhi = "保存";

            ViewState["id"] = "0";
            ViewState["strRawHtml"] = string.Empty;
            ViewState["strint"] = "0";
            ViewState["strDishHtml"] = string.Empty;
            ViewState["strTableHtml"] = string.Empty;
            if (!IsPostBack)
            {

              
                voc.TreeViewDataBing_RawMaterials(Tv_RawMaterial);
                voc.TreeViewDataBind_DishTypes(Tv_Dish, "1");
               
                voc.DropDownListDataBind_library(Dr_library);
               
               

                if (Dr_library.Items.Count > 0)
                {
                    voc.DropDownListDataBind_DishTypes(Dr_DishType, Dr_library.Items[0].Value);
                }
               //ViewState["strRawHtml"]= cmo.GetRawType();
               // ViewState["strDishHtml"]=cmo.GetDishType();
            }
        }
       
       
        protected void ButLibrary_Click(object sender, EventArgs e)
        {
            string str = strLibrary;
            voc.TreeViewDataBind_DishTypes(Tv_Dish, strLibrary);   
        }
        protected void ButClear_Dish_Click(object sender, EventArgs e)
        {
            this.Txt_caiming.Text = "";
            this.Txt_tedian.Text = "";
            this.Txt_zuofa.Text = "";
            Session["strTableHtml"] = null;
            dt.Clear();
            strTableHtml = string.Empty;

            this.lbl_reliang.Text = string.Empty;
            this.lbl_danbaizhi.Text = string.Empty;
            this.lbl_zhifang.Text = string.Empty;
            this.lbl_tanshuihuahewu.Text = string.Empty;
            this.lbl_shanshixianwei.Text = string.Empty;
            this.lbl_weishengsuA.Text = string.Empty;
            this.lbl_weishengsuB1.Text = string.Empty;
            this.lbl_weishengsuB2.Text = string.Empty;
            this.lbl_weishengsuC.Text = string.Empty;
            this.lbl_weishengsuE.Text = string.Empty;
            this.lbl_shihuangchundangliang.Text = string.Empty;
            this.lbl_nikesuan.Text = string.Empty;
            this.lbl_huluobosu.Text = string.Empty;
            this.lbl_gai.Text = string.Empty;
            this.lbl_ling.Text = string.Empty;
            this.lbl_jia.Text = string.Empty;
            this.lbl_na.Text = string.Empty;
            this.lbl_mei.Text = string.Empty;
            this.lbl_tie.Text = string.Empty;
            this.lbl_xin.Text = string.Empty;
            this.lbl_xi.Text = string.Empty;
            this.lbl_tong.Text = string.Empty;
            this.lbl_men.Text = string.Empty;
            this.lbl_dian.Text = string.Empty;
        }
       

        protected void Tv_RawMaterial_SelectedNodeChanged(object sender, EventArgs e)
        {
            TreeNode tn = Tv_RawMaterial.SelectedNode;
            Catering_Rules.Species crs = new Catering_Rules.Species();
            Catering_Rules.T_RawMaterial crr = new Catering_Rules.T_RawMaterial();
            Catering_Model.E_RawMaterial cmr = crr.GetModel(Convert.ToInt32(tn.Value));
            Catering_Model.Species cms = crs.GetModel(cmr.RAWZS);
            Catering.View_Operaction.Caipin_Manage_Operaction cmo = new View_Operaction.Caipin_Manage_Operaction();
            string str = tn.Target;


            i = i + 1;
            if (Session["strTableHtml"] == null)
            {
                Session["strTableHtml"] =
                    cmo.GetTableHtml(i, tn.Text, cms.TypeName, tn.Target);
            }
            else
            {
                Session["strTableHtml"] = Session["strTableHtml"].ToString() +
                    cmo.GetTableHtml(i, tn.Text, cms.TypeName, tn.Target);
            }


            cmo.setDataToDataTable(dt, i, tn.Text, cms.TypeName, tn.Target, tn.Value);
            setLableValue(tn.Value);
        }
        #region 微量元素
        #region 存放微量元素
        static Int32 reliang = 0;
        static Int32 danbaizhi = 0;
        static Int32 zhifang = 0;
        static Int32 tanshuihuahewu = 0;
        static Int32 shanshixianwei = 0;
        static Int32 weishengsuA = 0;
        static Int32 weishengsuB1 = 0;
        static Int32 weishengsuB2 = 0;
        static Int32 weishengsuC = 0;
        static Int32 weishengsuE = 0;
        static Int32 shihuangchundangliang = 0;
        static Int32 nikesuan = 0;
        static Int32 huluobosu = 0;

        static Int32 gai = 0;
        static Int32 ling = 0;
        static Int32 jia = 0;
        static Int32 na = 0;
        static Int32 mei = 0;
        static Int32 tie = 0;
        static Int32 xin = 0;
        static Int32 xi = 0;
        static Int32 tong = 0;
        static Int32 men = 0;
        static Int32 dian = 0;
        #endregion
        /// <summary>
        /// 给菜品添加营养素值
        /// </summary>
        /// <param name="strRawID"></param>
        public void setLableValue(string strRawID)
        {
            Catering_Rules.ViewOperaction crvo = new Catering_Rules.ViewOperaction();
            int num = 100;
            Catering.View_Operaction.Caipin_Manage_Operaction cmo = new View_Operaction.Caipin_Manage_Operaction();
            DataTable dt = cmo.Nutrient_DataSetToDatatabl();
            if (dt != null && dt.Rows.Count > 0)
            {
                DataRow[] dr_Nutrient = dt.Select("RaWId=" + strRawID);

                reliang = reliang + Convert.ToInt32(dr_Nutrient[0][1]) * num;
                this.lbl_reliang.Text = reliang.ToString();

                danbaizhi = danbaizhi + Convert.ToInt32(dr_Nutrient[0][2]) * num;
                this.lbl_danbaizhi.Text = danbaizhi.ToString();

                zhifang = zhifang + Convert.ToInt32(dr_Nutrient[0][3]) * num;
                this.lbl_zhifang.Text = zhifang.ToString();

                tanshuihuahewu = tanshuihuahewu + Convert.ToInt32(dr_Nutrient[0][4]) * num;
                this.lbl_tanshuihuahewu.Text = tanshuihuahewu.ToString();

                shanshixianwei = shanshixianwei + Convert.ToInt32(dr_Nutrient[0][5]) * num;
                this.lbl_shanshixianwei.Text = shanshixianwei.ToString();

                weishengsuA = weishengsuA + Convert.ToInt32(dr_Nutrient[0][6]) * num;
                this.lbl_weishengsuA.Text = weishengsuA.ToString();

                weishengsuB1 = weishengsuB1 + Convert.ToInt32(dr_Nutrient[0][7]) * num;
                this.lbl_weishengsuB1.Text = weishengsuB1.ToString();

                weishengsuB2 = weishengsuB2 + Convert.ToInt32(dr_Nutrient[0][8]) * num;
                this.lbl_weishengsuB2.Text = weishengsuB2.ToString();

                weishengsuC = weishengsuC + Convert.ToInt32(dr_Nutrient[0][9]) * num;
                this.lbl_weishengsuC.Text = weishengsuC.ToString();

                weishengsuE = weishengsuE + Convert.ToInt32(dr_Nutrient[0][10]) * num;
                this.lbl_weishengsuE.Text = weishengsuE.ToString();

                shihuangchundangliang = shihuangchundangliang + Convert.ToInt32(dr_Nutrient[0][11]) * num;
                this.lbl_shihuangchundangliang.Text = shihuangchundangliang.ToString();

                nikesuan = nikesuan + Convert.ToInt32(dr_Nutrient[0][12]) * num;
                this.lbl_nikesuan.Text = nikesuan.ToString();

                huluobosu = huluobosu + Convert.ToInt32(dr_Nutrient[0][13]) * num;
                this.lbl_huluobosu.Text = huluobosu.ToString();

                gai = gai + Convert.ToInt32(dr_Nutrient[0][14]) * num;
                this.lbl_gai.Text = gai.ToString();

                ling = ling + Convert.ToInt32(dr_Nutrient[0][15]) * num;
                this.lbl_ling.Text = ling.ToString();

                jia = jia + Convert.ToInt32(dr_Nutrient[0][16]) * num;
                this.lbl_jia.Text = jia.ToString();

                na = na + Convert.ToInt32(dr_Nutrient[0][17]) * num;
                this.lbl_na.Text = na.ToString();

                mei = mei + Convert.ToInt32(dr_Nutrient[0][18]) * num;
                this.lbl_mei.Text = mei.ToString();

                tie = tie + Convert.ToInt32(dr_Nutrient[0][19]) * num;
                this.lbl_tie.Text = tie.ToString();

                xin = xin + Convert.ToInt32(dr_Nutrient[0][20]) * num;
                this.lbl_xin.Text = xin.ToString();

                xi = xi + Convert.ToInt32(dr_Nutrient[0][21]) * num;
                this.lbl_xi.Text = xi.ToString();

                tong = tong + Convert.ToInt32(dr_Nutrient[0][22]) * num;
                this.lbl_tong.Text = tong.ToString();

                men = men + Convert.ToInt32(dr_Nutrient[0][23]) * num;
                this.lbl_men.Text = men.ToString();

                dian = dian + Convert.ToInt32(dr_Nutrient[0][24]) * num;
                this.lbl_dian.Text = dian.ToString();

            }



        }
        #endregion
        protected void SelectedIndexChanged(object sender, EventArgs e)
        {
            voc.DropDownListDataBind_DishTypes(Dr_DishType, this.Dr_library.SelectedValue);
        }

        protected void Dr_library_SelectedIndexChanged(object sender, EventArgs e)
        {
            voc.DropDownListDataBind_DishTypes(Dr_DishType, this.Dr_library.SelectedValue);
        }
        protected void bt_zengjia_Click(object sender, EventArgs e)
        {
            #region 菜信息插入
            Mdh.Name = Txt_caiming.Text.Trim();
            Mdh.Characteristic = Txt_tedian.Text.Trim();
            Mdh.FMID = Convert.ToInt32(Dr_library.Text);
            Mdh.DTID = Convert.ToInt32(Dr_DishType.Text);
            Mdh.Method = Txt_zuofa.Text.Trim();
            int DDID = _BllDish.Addd(Mdh);
            Response.Write(DDID);
            #endregion
            // 插入菜品食物信息
            #region 主要膳食营养含量
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i]["id"] != null)
                    {
                        _ModelDishInfo = new Catering_Model.E_DishInfo();
                        _ModelDishInfo.id = _BllDishInfo.GetMaxId();
                        _ModelDishInfo.DID = DDID;
                        _ModelDishInfo.DName = dt.Rows[i]["Food"].ToString();
                        _ModelDishInfo.RawID = 1;
                        _ModelDishInfo.Re = int.Parse(dt.Rows[i]["re"].ToString());
                        _ModelDishInfo.ZS = dt.Rows[i]["zs"].ToString();
                        _BllDishInfo.Add(_ModelDishInfo);
                    }
                }
            }
            #endregion
            // 插入其他微量元素
            #region 营养成分信息插入

            DishNews(DDID, lbl_reliang.Text, "热量");
            DishNews(DDID, lbl_danbaizhi.Text, "蛋白质");
            DishNews(DDID, lbl_zhifang.Text, "脂肪");
            DishNews(DDID, lbl_tanshuihuahewu.Text, "碳水化合物");
            DishNews(DDID, lbl_weishengsuA.Text, "膳食纤维");
            DishNews(DDID, lbl_danbaizhi.Text, "维生素A");
            DishNews(DDID, lbl_weishengsuB1.Text, "维生素B1");
            DishNews(DDID, lbl_weishengsuB2.Text, "维生素B2");
            DishNews(DDID, lbl_weishengsuC.Text, "维生素C");
            DishNews(DDID, lbl_weishengsuE.Text, "维生素E");
            DishNews(DDID, lbl_shihuangchundangliang.Text, "视黄醇当量");
            DishNews(DDID, lbl_nikesuan.Text, "尼克酸");
            DishNews(DDID, lbl_huluobosu.Text, "胡萝卜素");
            DishNews(DDID, lbl_gai.Text, "钙");
            DishNews(DDID, lbl_ling.Text, "磷");
            DishNews(DDID, lbl_jia.Text, "钾");
            DishNews(DDID, lbl_na.Text, "钠");
            DishNews(DDID, lbl_mei.Text, "镁");
            DishNews(DDID, lbl_tie.Text, "铁");
            DishNews(DDID, lbl_xin.Text, "锌");
            DishNews(DDID, lbl_xi.Text, "硒");
            DishNews(DDID, lbl_tong.Text, "铜");
            DishNews(DDID, lbl_men.Text, "锰");
            DishNews(DDID, lbl_dian.Text, "镁");
            DishNews(DDID, lbl_tie.Text, "碘");
            //新写的
            Exec();
            Response.Redirect("Caipu_Manage.aspx");
            #endregion
        }
        public void DishNews(int ID, string Name, string Lblname)
        {
            Catering_Model.DishNutrien CMDN = new Catering_Model.DishNutrien();//元
            CMDN.Dishid = ID;                                                  //素
            CMDN.Content = Lblname;                                            //表
            CMDN.element_name = Name;
            CRDN.Add(CMDN);
            mdnlist.Add(CMDN);
        }
        public void Exec()
        {
            CRDN.TransAdd(mdnlist);
            CRDN.ExecTrans();
        }
        #region 上传图片
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void butUpClose_Click(object sender, EventArgs e)
        {
            //ImgUpPoto.ImageUrl = cmo.UpImage(this.File_Upload);
        }

       
        #endregion
    }
}